package com.seventh.parkingrent.controller;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.seventh.parkingrent.alipay.config.AlipayConfig;
import com.seventh.parkingrent.common.Result;
import com.seventh.parkingrent.controller.form.RentMessageForm;
import com.seventh.parkingrent.domain.ParkingSpace;
import com.seventh.parkingrent.domain.PropertyManager;
import com.seventh.parkingrent.domain.RentMessage;
import com.seventh.parkingrent.domain.User;
import com.seventh.parkingrent.domain.Vo.ParkingSpaceVo;
import com.seventh.parkingrent.service.impl.ParkingSpaceServiceImpl;
import com.seventh.parkingrent.service.impl.RentMessageServiceImpl;
import com.seventh.parkingrent.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 订单详情记录表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-08-24
 */
@RestController
@RequestMapping("")
@CrossOrigin("*")
@Slf4j
public class RentMessageController {
    @Autowired
    private RentMessageServiceImpl rentMessageService;
    @Autowired
    private ParkingSpaceServiceImpl parkingSpaceService;
    @Autowired
    private UserServiceImpl userService;

    @GetMapping("getallParkingSpace")
    public Result getallParkingSpace() {
        log.info("获取全部未出租的订单信息");
        List<RentMessage> allNotRent = rentMessageService.getAllNotRent();

        List<ParkingSpaceVo> parkingSpaceVos = new ArrayList<>();
        for (RentMessage rentMessage : allNotRent) {
            //创建一个返回对象Vo,订单信息赋值
            ParkingSpaceVo parkingSpaceVo = new ParkingSpaceVo();
            parkingSpaceVo.setRentStartTime(rentMessage.getRentStartTime());
            parkingSpaceVo.setRentEndTime(rentMessage.getRentEndTime());
            parkingSpaceVo.setCost(rentMessage.getCost());
            parkingSpaceVo.setLessorId(rentMessage.getLessorId());
            parkingSpaceVo.setRentMessageId(rentMessage.getRentMessageId());
            Integer id = rentMessage.getSpacingSpaceId();
            //根据停车场id获取停车场信息
            ParkingSpace parkingSpace = parkingSpaceService.getParkingSpaceByid(id);
            //把需要的信息赋给返回对象,车位信息赋值
            parkingSpaceVo.setPointY(parkingSpace.getPointY());
            parkingSpaceVo.setPointX(parkingSpace.getPointX());
            parkingSpaceVo.setParkingSpaceAddr(parkingSpace.getParkingSpaceAddr());
            parkingSpaceVo.setParkingSpacePic(parkingSpace.getParkingSpacePic());
            parkingSpaceVos.add(parkingSpaceVo);
        }
        return Result.success(parkingSpaceVos);
    }

    @GetMapping("updateStatusById")
    public Result updateStatusById(Integer id, Integer tenantryid, HttpServletResponse response) throws IOException {
        log.info("根据订单id:{}修改租赁人id:{}以及状态", id, tenantryid);
        //根据订单id查询状态如果不是0则租赁失败
        boolean bool = rentMessageService.getStatusById(id);
        if (!bool) {
            return Result.fail();
        }
        //修改状态
        boolean b = rentMessageService.updateStatusById(id, tenantryid);
        return b ? Result.success() : Result.fail();
    }




    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody RentMessage rentMessage) {
        Boolean bool=true;
        System.out.println(rentMessage.getSpacingSpaceId());
        List<RentMessage> list=rentMessageService.findOrderTime(rentMessage.getSpacingSpaceId());
        for(RentMessage a:list){
            System.out.println("开始"+a.getRentStartTime().getTime());
            System.out.println("结束"+a.getRentEndTime().getTime());
            System.out.println("选择结束"+rentMessage.getRentEndTime().getTime());
            System.out.println("选择开始"+rentMessage.getRentStartTime().getTime());
            if((a.getRentStartTime().getTime()>rentMessage.getRentEndTime().getTime())||(a.getRentEndTime().getTime()<rentMessage.getRentStartTime().getTime())){
                bool=true;

            }else {
                bool=false;
            }

        }
            if(bool){
                Integer result = rentMessageService.addOrder(rentMessage);
                if (result!=null){
                    return Result.success("发布订单成功");
                }else {
                    return Result.fail("发布订单失败");
                }

            }else{
                return Result.fail("时间有冲突");
            }


    }

    @GetMapping("payBywellt")
    public Result payBywellt(Integer id,Integer tenantryid,Double money){
        log.info("得到订单id:{},用户id:{},租金:{}",id,tenantryid,money);
        boolean bool = rentMessageService.getStatusById(id);
        if (!bool) {
            return Result.fail("已经被别人抢单了哦");
        }
        User user = userService.getUserById(tenantryid);
        double v = user.getBalance().doubleValue();
        if(v<money){
            return Result.fail("余额不足请充值");
        }
        userService.updatebalance(v - money, tenantryid);
        rentMessageService.okStatusAndtenantryId(id,tenantryid);
        return Result.success("租赁成功");
    }


    @GetMapping("/findRent")
    public Result findRent(@RequestParam(name = "pageSize", defaultValue = "3") int pageSize,
                           @RequestParam(name = "current", defaultValue = "1") int current,int lessorId){

        log.info("进入分页查询方法{},{},{}",lessorId,pageSize,current);
        IPage<RentMessageForm> page = rentMessageService.findRent(lessorId, pageSize, current);
        return Result.success(page);
    }
    @GetMapping("getallleaseByid")
    public Result getallleaseByid(Integer userid){
        log.info("得到用户id:{}",userid);
        List<RentMessage> rentMessages = rentMessageService.getallleaseByid(userid);
        List<ParkingSpaceVo> parkingSpaceVos = new ArrayList<>();
        for (RentMessage rentMessage : rentMessages) {
            Integer spacingSpaceId = rentMessage.getSpacingSpaceId();
            ParkingSpace space = parkingSpaceService.getParkingSpaceByid(spacingSpaceId);
            ParkingSpaceVo parkingSpaceVo = new ParkingSpaceVo();
            //设置订单需要的数据
            parkingSpaceVo.setRentStartTime(rentMessage.getRentStartTime());
            parkingSpaceVo.setRentEndTime(rentMessage.getRentEndTime());
            parkingSpaceVo.setRentMessageId(rentMessage.getRentMessageId());
            parkingSpaceVo.setLessorId(space.getUserId());
            parkingSpaceVo.setCost(rentMessage.getCost());
            parkingSpaceVo.setParkingSpaceAddr(space.getParkingSpaceAddr());
            parkingSpaceVos.add(parkingSpaceVo);
        }
        return Result.success(parkingSpaceVos);
    }

    @GetMapping("/deleteRow")
    public Result deleteRow(int rentMessageId) {
        log.info("进入接口{}",rentMessageId);
        boolean row = rentMessageService.deleteRow(rentMessageId);
        if (row) {
            return Result.success("撤回成功");
        }
        return Result.fail("撤回失败");
    }

    @GetMapping("/findRentId")
    public Result findRentId(int rentMessageId){
    log.info("查询方法{}",rentMessageId);
   return Result.success(rentMessageService.findById(rentMessageId));
    }
}

